package com.cnpinxi.controller.web.order;

import com.cnpinxi.controller.base.BaseController;
import com.cnpinxi.def.Constant;
import com.cnpinxi.domain.ShoppingCart;
import com.cnpinxi.domain.User;
import com.cnpinxi.myRunTime.MyRunTimeExcption;
import com.cnpinxi.result.ResultObj;
import com.cnpinxi.service.ShoppingCartService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//import com.github.pagehelper.PageHelper;
//import com.github.pagehelper.PageInfo;

/**
 * Created by baggio on 2016/12/27.
 */
@RequestMapping("/shoppingCart")
@Controller
public class ShoppingCartController extends BaseController {

    @Autowired
    private ShoppingCartService shoppingCartService;


    @RequestMapping(value = "/shoppingCartList")
    public ModelAndView shoppingCart(HttpServletRequest request, HttpServletResponse response) {
        // source: 1(来源于网站) | 2(来源于微站)
        ModelAndView mov = new ModelAndView();
        Map<String,String> map=super.showRequestParameters(request);
        mov.setViewName("/web/order/shoppingCart");
        if(super.getIsMobile(request)){
            mov.setViewName("/wap/order/shoppingCart");
            super.oauth2(request, response);
            HttpSession session = request.getSession();
            try {
                User user=(User)session.getAttribute(Constant.SESSION_KEY_USER_LOGIN);
                if(user==null){
                    String openId = (String) session.getAttribute(Constant.SESSION_OPEN_ID);
                    if(StringUtils.isNotBlank(openId)){
                          user = super.getUserByOpenId(openId);
                        session.setAttribute(Constant.SESSION_KEY_USER_LOGIN, user);
                    }
                    map.put("userId",String.valueOf(user.getTid()));

                }
if(user!=null){
    List<ShoppingCart> shoppingCartList=  shoppingCartService.selectShoppingCartList(map);
    mov.addObject("shoppingCartList",shoppingCartList);
}


            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return mov;
    }

    @RequestMapping(value = "/modifyShoppingCart")
    @ResponseBody
    public String modifyShoppingCart(HttpServletRequest request,ShoppingCart shoppingCart) {
        ResultObj resultObj = new ResultObj();
        resultObj.setCode(ResultObj.CODE_SUCCESS);
        try {
User user=(User )request.getSession().getAttribute(Constant.SESSION_KEY_USER_LOGIN);
if(user==null){
    throw new MyRunTimeExcption("用户没有登录！");
}
shoppingCart.setUserId(user.getTid());
shoppingCartService.modifyShoppingCart(shoppingCart);

        } catch (MyRunTimeExcption runTimeExcption) {
            resultObj.setCode(ResultObj.CODE_FAIL);
            resultObj.setMsg(runTimeExcption.getMessage());
            runTimeExcption.printStackTrace();
        } catch (Exception e) {
            resultObj.setCode(ResultObj.CODE_FAIL);
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        logger.info("resultObj==>" + resultObj.toJson());
        return resultObj.toJson();
    }


    @RequestMapping(value = "/cancelShoppingCart")
    @ResponseBody
    public String cancelShoppingCart(HttpServletRequest request, Long shoppingCartId) {
        ResultObj resultObj = new ResultObj();
        resultObj.setCode(ResultObj.CODE_SUCCESS);
        try {
            Map<String,String> map=new HashMap<>();
            map.put("shoppingCartId",String.valueOf(shoppingCartId));
            shoppingCartService.deleteShoppingCart(map);
        } catch (MyRunTimeExcption runTimeExcption) {
            resultObj.setCode(ResultObj.CODE_FAIL);
            resultObj.setMsg(runTimeExcption.getMessage());
            runTimeExcption.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultObj.toJson();
    }









}
